package com.utooo.service.purchase;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.utooo.dao.purchase.RefundApplicationMapper;
import com.utooo.pojo.domain.purchase.order.PrepaymentApplicationDO;
import com.utooo.pojo.domain.purchase.order.RefundApplicationDO;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

@Service
public class RefundApplicationServiceImpl extends ServiceImpl<RefundApplicationMapper, RefundApplicationDO>
        implements RefundApplicationService {
    @Override
    public String getRefundNo() {
        String prefix = "TK";
        // 获取当前日期
        LocalDate currentDate = LocalDate.now();
        String datePart = currentDate.format(DateTimeFormatter.ofPattern("yyyyMMdd"));

        // 查询当天已有的最大订单号
        LambdaQueryWrapper<RefundApplicationDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.likeRight(RefundApplicationDO::getRefundApplicationNo, prefix + datePart);
        queryWrapper.orderByDesc(RefundApplicationDO::getRefundApplicationNo);
        queryWrapper.last("LIMIT 1");
        RefundApplicationDO refundApplicationDO = this.getOne(queryWrapper);

        // 生成序列号
        int maxSequence = 0;
        if (refundApplicationDO != null) {
            String refundApplicationNo = refundApplicationDO.getRefundApplicationNo();
            maxSequence = Integer.parseInt(refundApplicationNo.substring((prefix + datePart).length()));
        }
        int nextSequence = maxSequence + 1;
        String sequencePart = String.format("%03d", nextSequence);
        return prefix + datePart + sequencePart;
    }
}